﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;
namespace Booking_Hotel
{
    public partial class Supplier_Room : System.Web.UI.Page
    {
        Supplier_Hotel_Room entities = new Supplier_Hotel_Room();
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        int PhongID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                
                ShowPhong();
            }
        }
        protected void ShowPhong()
        {
            var phong = from dv in tbk.Supplier_Hotels
                            join shl in tbk.Supplier_Hotel_LoaiPhongs on dv.Supplier_HotelID equals shl.Supplier_HotelID
                            join shr in tbk.Supplier_Hotel_Rooms on shl.Supplier_Hotel_LoaiPhongID equals shr.Supplier_Hotel_LoaiPhongID
                            where dv.SupplierID == (int)Session["ss_SupplierID"]
                            select new
                            {
                                Supplier_Hotel_RoomID= shr.Supplier_Hotel_RoomID,
                                Supplier_Hotel_LoaiPhongID = shr.Supplier_Hotel_LoaiPhongID,
                                TenLoaiPhong = shl.TenLoaiPhong,
                                Supplier_HotelID = shl.Supplier_HotelID,
                                TenKhachSan = dv.TenKhachSan,
                                TenPhong= shr.TenPhong,
                                SoGiuong= shr.SoGiuong,
                                SoNguoiLon= shr.SoNguoiLon,
                                SoTreEm= shr.SoTreEm,
                                Description = shr.Description,
                                TrangThaiLoaiPhong = shr.TrangThaiLoaiPhong
                            };
            GridView1.DataSource = phong;
            GridView1.DataBind();

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_RoomID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[3].Text.Trim();
            DDL_TenLoaiPhong.SelectedValue = select.Cells[1].Text.Trim();
            txt_TenPhong.Text = select.Cells[5].Text.Trim();
            txt_SoGiuong.Text = select.Cells[6].Text.Trim();
            txt_SoNguoiLon.Text = select.Cells[7].Text.Trim();
            txt_SoTreEm.Text = select.Cells[8].Text.Trim();
            txt_Desciption.Text = select.Cells[9].Text.Trim();
            CB_TrangThaiPhong.Checked = ((CheckBox)select.Cells[10].Controls[0]).Checked;
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                //e.Row.Cells[0].Visible = false;
                //e.Row.Cells[1].Visible = false;
                //e.Row.Cells[3].Visible = false;

            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            PhongID = int.Parse(row.Cells[0].Text.Trim());//Supplier_Hotel_DichVuID trên lưới.
            Supplier_Hotel_Room shr = tbk.Supplier_Hotel_Rooms.Where(c => c.Supplier_Hotel_RoomID == PhongID).Single();
            tbk.Supplier_Hotel_Rooms.DeleteOnSubmit(shr);
            tbk.SubmitChanges();
            ShowPhong();
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel_Room();
                entities.Supplier_Hotel_LoaiPhongID = int.Parse(DDL_TenLoaiPhong.SelectedValue);
                entities.TenPhong = txt_TenPhong.Text.Trim();
                entities.SoGiuong = Byte.Parse(txt_SoGiuong.Text.Trim());
                entities.SoNguoiLon = Byte.Parse(txt_SoNguoiLon.Text.Trim());
                entities.SoTreEm = Byte.Parse(txt_SoTreEm.Text.Trim());
                entities.Description = txt_Desciption.Text.Trim();
                entities.TrangThaiLoaiPhong = CB_TrangThaiPhong.Checked;
                Supplier_RoomDAO sr = new Supplier_RoomDAO();
                sr.addSupplier_Hotel_Room(entities);
                ShowPhong();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_RoomID = (int)(Session["ss_Supplier_Hotel_RoomID"]);
                if (check_Supplier_Hotel_RoomID != null)
                {
                    entities = new Supplier_Hotel_Room();
                    entities.Supplier_Hotel_RoomID = check_Supplier_Hotel_RoomID;
                    entities.Supplier_Hotel_LoaiPhongID = int.Parse(DDL_TenLoaiPhong.SelectedValue);
                    entities.TenPhong = txt_TenPhong.Text.Trim();
                    entities.SoGiuong = Byte.Parse(txt_SoGiuong.Text.Trim());
                    entities.SoNguoiLon = Byte.Parse(txt_SoNguoiLon.Text.Trim());
                    entities.SoTreEm = Byte.Parse(txt_SoTreEm.Text.Trim());
                    entities.Description = txt_Desciption.Text.Trim();
                    entities.TrangThaiLoaiPhong = CB_TrangThaiPhong.Checked;
                    Supplier_RoomDAO sr = new Supplier_RoomDAO();
                    sr.updateSupplier_Hotel_Room(entities);
                    ShowPhong();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void DDL_TenKhachSan_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDL_TenLoaiPhong.Items.Clear();
            //Session["ss_Supplier_Hotel"] = DDL_TenKhachSan.SelectedValue;
        }

       
    }
}